package cn.enited.devtools.apiaggregator.controller;

import cn.enited.devtools.apiaggregator.dto.ActionResult;
import cn.enited.devtools.apiaggregator.dto.ApiAggObjectDto;
import cn.enited.devtools.apiaggregator.dto.ObjectActionResult;
import cn.enited.devtools.apiaggregator.dto.PageActionResult;
import cn.enited.devtools.apiaggregator.service.ApiConfService;
import cn.enited.devtools.apiaggregator.utils.ActionUtil;
import cn.enited.devtools.apiaggregator.validator.group.CreateValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.FindValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.PagingValidationGroup;
import cn.enited.devtools.apiaggregator.validator.group.UpdateValidationGroup;
import cn.enited.devtools.apiaggregator.vo.ApiAggObjectParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/conf")
public class ConfController extends BaseController {

    private ApiConfService confService;

    @Autowired
    public void setConfService(ApiConfService confService) {
        this.confService = confService;
    }

    @GetMapping(path = "/get/{id}")
    public ObjectActionResult<ApiAggObjectDto> get(@PathVariable("id") Long id) {
        return ActionUtil.objectResult(confService.get(id));
    }

    @GetMapping(path = "/getByName/{projectName}/{apiName}")
    public ObjectActionResult<ApiAggObjectDto> getByName(@PathVariable("projectName") String projectName,
                                                         @PathVariable("apiName") String apiName) {
        return ActionUtil.objectResult(confService.getByName(projectName, apiName));
    }

    @PostMapping(path = "/page")
    public PageActionResult<ApiAggObjectDto> page(
            @RequestBody @Validated({FindValidationGroup.class, PagingValidationGroup.class}) ApiAggObjectParamVo paramVo) {
        return ActionUtil.pageResult(confService.findPage(paramVo));
    }

    @PostMapping(path = "/add")
    public ObjectActionResult<Long> add(@RequestBody @Validated({CreateValidationGroup.class}) ApiAggObjectDto param) {
        return ActionUtil.objectResult(confService.add(param));
    }

    @PostMapping(path = "/update")
    public ActionResult update(@RequestBody @Validated({UpdateValidationGroup.class}) ApiAggObjectDto param) {
        confService.update(param);
        return ActionUtil.success();
    }

    @PostMapping(path = "/delete/{id}")
    public ActionResult delete(@PathVariable("id") Long id) {
        confService.delete(id);
        return ActionUtil.success();
    }
}
